#include "common.h"
#include "globals.h"
#include "logger.h"
#include "shared_func.h"
#include "termlist.h"

//-----globals---------
FILE* g_input;
char* fconf="./conf/displayGen.ini";
Globs g;
Stats s;
//---------------------

void usage() {
  fprintf(stderr, "\n");
  fprintf(stderr, "Project    : %s\n", PROJECT_NAME);
  fprintf(stderr, "BuildDate  : %s\n", BuildDate);
  fprintf(stderr, "Description: %s\n", DESCRIPTION);
  fprintf(stderr, "Usage: ./displayGen\n");
  fprintf(stderr, "\n");
}

static int init_config() {
  IniContext iniContext;
  int ret;
  char* cstr;

  if((ret=iniLoadFromFile(fconf, &iniContext)) != 0) {
    FATAL("error load descriptor file:%s",
	fconf);
    return E_RET;
  }

  set_log_level(iniGetStrValue(NULL, "log_level", &iniContext));

  cstr = iniGetStrValue(NULL, "in_data_path", &iniContext);
  if(cstr == NULL) {
    FATAL("error get value : in_data_path");
    return E_RET;
  }
  strcpy(g.in_data_path,
      cstr);

  cstr = iniGetStrValue(NULL, "out_data_path", &iniContext);
  if(cstr == NULL) {
    FATAL("error get value : out_data_path");
    return E_RET;
  }
  strcpy(g.out_data_path,
      cstr);

  return S_RET;
}

int init() {
  int ret;
  log_init();
  g_log_context.log_level = LOG_DEBUG;
  memset(&g, 0, sizeof(g));
  memset(&s, 0, sizeof(s));
  
  ret = init_config();
  if(ret != S_RET) {
    FATAL("read config error");
    exit(-1);
  }

  return S_RET;
}

void write_didx(int fid, uint32_t length, uint32_t off, FILE* ofp) {
  //NOTICE("write:[%d][%u][%u]",
  //    fid, length, off);
  DisplayIdx display;
  display.fno = fid;
  display.length = length;
  display.foff = off;
  int ret;

  ret = fwrite(&display, sizeof(DisplayIdx), 1, ofp);
  assert(ret == 1);

}

void do_it() {
  int ret;
  char fname[NAME_LEN];
  FILE* fp;
  DisplayFix fix;
  char* disBuff;
  int fid = 0;
  FILE* ofp;

  disBuff = (char*)malloc(MAX_DISPLAY_SIZE);
  assert(disBuff);

  sprintf(fname, "%s/%s", 
      g.out_data_path,
      DISPLAY_IDX_NAME);
  ofp = fopen(fname, "w");
  if(ofp == NULL) {
    FATAL("error :fopen [%s] for writing error",
	fname);
    exit(-1);
  }

  //open 1st
  sprintf(fname, "%s/%s.0",
      g.in_data_path,
      DISPLAY_NAME);
  fp = fopen(fname, "r");
  if(fp == NULL) {
    FATAL("error: fopen [%s] error",
	fname);
    exit(-1);
  }

  while(1) {
    ret = fread(&fix, sizeof(DisplayFix), 1, fp);
    if(ret != 1) {
      if(feof(fp)) {
        NOTICE("finish reading [%s]", fname);
	fclose(fp);
	fid++;
	sprintf(fname, "%s/%s.%d",
	    g.in_data_path,
	    DISPLAY_NAME,
	    fid);
	fp = fopen(fname, "r");
	if(fp == NULL) {
	  NOTICE("end at[%s]", fname);
	  break;
	}
	ret = fread(&fix, sizeof(DisplayFix), 1, fp);
	assert(ret == 1);
      }
      else assert(0);
    }

    ret = fread(disBuff, fix.size, 1, fp);
    assert(ret == 1);

    write_didx(fid, fix.size, ftell(fp), ofp);
  }
}


int main(int argc, char** argv) {
  int ret;

  g_input = stdin;

  ret = init();
  if(ret != S_RET) {
    FATAL("error init system");
    exit(-1);
  }

  if(argc == 2) {
    if(strcmp(argv[1], "-v") == 0 ||
	strcmp(argv[1], "--version") == 0 ||
	strcmp(argv[1], "-h") == 0) {
      usage();
      exit(-1);
    }
    g.mapid = atoi(argv[1]);
  }
  else if(argc == 1) {
    g.mapid = 0;
  }
  else {
    usage();
    exit(-1);
  }
  
  do_it();

  return S_RET;
}
